package com.kinghunt.misp.service.impl.devicex;

import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.devicex.DeviceDTO;
import com.kinghunt.misp.common.param.devicex.DeviceParam;
import com.kinghunt.misp.dal.dao.devicex.DeviceDAO;
import com.kinghunt.misp.dal.model.custx.CustDO;
import com.kinghunt.misp.dal.model.devicex.DeviceDO;
import com.kinghunt.misp.service.api.devicex.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设备管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@Slf4j
@Service
@Transactional
public class DeviceServiceImpl implements DeviceService {

    @Autowired(required = false)
    private DeviceDAO deviceDAO;

    @Override
    public ApiResult<PageInfo<DeviceDTO>> getDeviceList(DeviceParam deviceParam) {
        int offset = (deviceParam.getPageNum() - 1) * deviceParam.getPageSize();
        DeviceDO deviceDO = ConvertUtil.convert(deviceParam, DeviceDO.class);
        int count = deviceDAO.selectDeviceListCount(deviceDO);
        PageInfo<DeviceDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(deviceParam.getPageNum());
        pageInfo.setPageSize(deviceParam.getPageSize());
        List<DeviceDO> deviceDOList = deviceDAO.selectDeviceList(deviceDO, offset, deviceParam.getPageSize());
        List<DeviceDTO> dto =  ConvertUtil.convert(deviceDOList, DeviceDTO.class);
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<DeviceDTO> getDeviceDetail(Long deviceId) {
        DeviceDO deviceDO = deviceDAO.selectByPrimaryKey(deviceId);
        DeviceDTO deviceDTO = ConvertUtil.convert(deviceDO, DeviceDTO.class);
        return ApiResult.ok(deviceDTO);
    }

    @Override
    public ApiResult addDevice(DeviceParam deviceParam) {
        //判断设备编号是否唯一
        List<DeviceDO> deviceDOList = deviceDAO.selectByDeviceNo(deviceParam.getDeviceNo());
        if(deviceDOList != null && deviceDOList.size() > 0){
            return ApiResult.failOfMessage("您新增的设备编号已存在！ ");
        }
        DeviceDO deviceDO = ConvertUtil.convert(deviceParam, DeviceDO.class);
        // 保存地址到数据库
        deviceDAO.insert(deviceDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult updateDevice(DeviceParam deviceParam) {
        DeviceDO deviceDO = deviceDAO.selectByPrimaryKey(deviceParam.getDeviceId());
        if(null == deviceDO){
            return ApiResult.failOfMessage("您修改的设备不存在！ ");
        }
        // 修改当前地址
        DeviceDO updateDeviceDO = ConvertUtil.convert(deviceParam, DeviceDO.class);
        deviceDAO.updateByPrimaryKeySelective(updateDeviceDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<Boolean> deleteDevice(Long deviceId) {
        DeviceDO deviceDO = deviceDAO.selectByPrimaryKey(deviceId);
        if(null == deviceDO){
            return ApiResult.failOfMessage("您删除的设备不存在！ ");
        }
        deviceDAO.deleteByPrimaryKey(deviceId);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<List<DeviceDTO>> getDeviceByIds(List<Long> deviceIds) {
        if (CollectionUtils.isEmpty(deviceIds)) {
            log.warn("getDeviceByIds 入参为空");
            return ApiResult.failOfMessage("您查找的设备参数不正确！ ");
        }
        List<DeviceDO> deviceDOList = deviceDAO.selectByIds(deviceIds);
        List<DeviceDTO> dto =  ConvertUtil.convert(deviceDOList, DeviceDTO.class);
        return ApiResult.ok(dto);
    }
}
